package com.zhu.day.util;

import com.alibaba.fastjson.JSON;
import com.oracle.tools.packager.Log;
import com.zhu.day.model.NewcomerAward;
import com.zhu.day.model.ResultsInfo;
import com.zhu.day.model.RevivalInfo;
import com.zhu.day.model.Stage;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
public class DoPromotionService {

    static String stageJson = "[\n" +
            "{\n" +
            "\"stageStart\":\"2021-04-22T00:00:00\",\n" +
            "\"stageEnd\" :\"2021-04-24T00:00:00\",\n" +
            "\"desc\":\"新人大赏\",\n" +
            "\"stageNo\": 1,\n" +
            "\"directPromotionStage\":3,\n" +
            "\"track\":{\n" +
            "\t\"1000\":-1,\n" +
            "\t\"1001\":-1\n" +
            "},\n" +
            "\"preStageNo\":0,\n" +
            "\"littlePromCount\" :-1,\n" +
            "\"bigProCount\" :-1,\n" +
            "\"baseCount\": -1,\n" +
            "\"ifFinal\" :false,\n" +
            "\"rouletteThumbMultiples\": \"1\",\n" +
            "\"normalRatio\": \"1\"\n" +
            "},\n" +
            "{\n" +
            "\"stageStart\":\"2021-04-24T00:00:00\",\n" +
            "\"stageEnd\" :\"2021-04-26T00:00:00\",\n" +
            "\"desc\":\"资格赛\",\n" +
            "\"stageNo\": 2,\n" +
            "\"directPromotionStage\":-1,\n" +
            "\"track\":{\n" +
            "\t\"1000\":100,\n" +
            "\t\"10001\":100\n" +
            "},\n" +
            "\"preStageNo\":0,\n" +
            "\"ifRepechage\":false,\n" +
            "\"littlePromCount\" :100,\n" +
            "\"bigProCount\" :100,\n" +
            "\"baseCount\":  -1,\n" +
            "\"ifFinal\":  false,\n" +
            "\"rouletteThumbMultiples\":  \"1\",\n" +
            "\"normalRatio\":  \"1\"\n" +
            "},\n" +
            "{\n" +
            "\"stageStart\":\"2021-04-26T00:00:00\",\n" +
            "\"stageEnd\" :\"2021-04-27T00:00:00\",\n" +
            "\"desc\":\"初赛\",\n" +
            "\"preStageNo\":2,\n" +
            "\"stageNo\": 3,\n" +
            "\"directPromotionStage\":-1,\n" +
            "\"track\":{\n" +
            "\t\"1000\":30,\n" +
            "\t\"10001\":30\n" +
            "},\n" +
            "\"littlePromCount\" :30,\n" +
            "\"bigProCount\" :30,\n" +
            "\"baseCount\":  100,\n" +
            "\"ifFinal\":  false,\n" +
            "\"rouletteThumbMultiples\":  \"1\",\n" +
            "\"normalRatio\":  \"1\"\n" +
            "},\n" +
            "{\n" +
            "\"stageStart\":\"2021-04-27T00:00:00\",\n" +
            "\"stageEnd\" :\"2021-04-28T00:00:00\",\n" +
            "\"desc\":\"半决赛\",\n" +
            "\"stageNo\":4,\n" +
            "\"preStageNo\":3,\n" +
            "\"directPromotionStage\":-1,\n" +
            "\"track\":{\n" +
            "\t\"1000\":10,\n" +
            "\t\"10001\":15\n" +
            "},\n" +
            "\"littlePromCount\" :10,\n" +
            "\"bigProCount\" :15,\n" +
            "\"baseCount\":  30,\n" +
            "\"ifFinal\":  false,\n" +
            "\"rouletteThumbMultiples\":  \"1\",\n" +
            "\"normalRatio\":  \"1\"\n" +
            "},\n" +
            "{\n" +
            "\"stageStart\":\"2021-04-28T00:00:00\",\n" +
            "\"stageEnd\" :\"2021-04-29T00:00:00\",\n" +
            "\"stageNo\": 5,\n" +
            "\"preStageNo\":4,\n" +
            "\"directPromotionStage\":-1,\n" +
            "\"track\":{\n" +
            "\t\"1002\":8\n" +
            "},\n" +
            "\"desc\":\"决赛\",\n" +
            "\"promotionCount\":  8,\n" +
            "\"baseCount\":  25,\n" +
            "\"ifFinal\":  true,\n" +
            "\"rouletteThumbMultiples\":  \"1\",\n" +
            "\"normalRatio\": \"1\"\n" +
            "}\n" +
            "]";

    public static void main(String[] args) {

//        doPromotionRank();
//        doPromotionVip();
        getResultsList();

    }

    public static  void getResultsList(){
        Optional<ResultsInfo> resultsInfo = DanceKingRankUtil.getResultsInfo();
        String testSysTime = "2021-04-29T02:00:00";
        LocalDateTime time = DanceKingRankUtil.getDateTimeNow(testSysTime);
        Optional<Boolean> ifShow = DanceKingRankUtil.ifShowResults(resultsInfo.get(), time);
        if (ifShow.isPresent() && ifShow.get()){
            System.out.println("展示赛果:"+ifShow.get());
        }else {
            System.out.println("展示赛果:"+ifShow.get());
        }
    }


    /**
     * 新人大赏直接晋级资格赛 方法
     */
    public static void doPromotionVip() {
        Optional<NewcomerAward> optionalNewcomerAward = DanceKingRankUtil.getNewcomerAward();
        NewcomerAward newcomerAward = optionalNewcomerAward.get();

        List<Stage> stages = JSON.parseArray(stageJson, Stage.class);
        String testSysTime = "2021-04-24T00:00:00";
        LocalDateTime nowTime = DanceKingRankUtil.getDateTimeNow(testSysTime);

        Optional<Stage> currentStage = DanceKingRankUtil.getCurrentStage(stages, nowTime);
        if (currentStage.isPresent()) {
            Stage stage = currentStage.get();
            Integer stageNo = stage.getStageNo();
            if (newcomerAward.getCurrentStage().equals(stageNo)) {
                System.out.println("当前赛段:" + stageNo + " 需要从新人大赏获取晋级主播,从:" + newcomerAward.getFromStage() + "赛段直接晋级到:" + newcomerAward.getToStage());
                //zset 中获取 newcomerAward.getFromStage() 的数据,插入newcomerAward.getToStage()中

                //发个 push


            } else {
                System.out.println("时间不符合");
            }
        }
    }
    //复活赛晋级
    public static void doPromotionRevival(){
        Optional<RevivalInfo> optionalRevivalInfo = DanceKingRankUtil.getRevivalInfo();
        if (optionalRevivalInfo.isPresent()){
            String testSysTime = "2021-04-28T00:00:00";
            LocalDateTime now = DanceKingRankUtil.getDateTimeNow(testSysTime);
            RevivalInfo revivalInfo = optionalRevivalInfo.get();
            Optional<Boolean> optionalBoolean = DanceKingRankUtil.ifRevival(revivalInfo, now);
            log.info("doPromotionRevival#optionalBoolean:{}",optionalBoolean);
            if (optionalBoolean.isPresent() && optionalBoolean.get()){

            }

        }


    }


    /**
     * 主榜单晋级
     */
    public static void doPromotionRank() {
        List<Stage> stages = JSON.parseArray(stageJson, Stage.class);
        String testSysTime = "2021-04-28T23:58:01";
        LocalDateTime time = DanceKingRankUtil.getDateTimeNow(testSysTime);

        Optional<Stage> currentStage = DanceKingRankUtil.getCurrentStage(stages, time);

        Map<Integer, Stage> stageMap = DanceKingRankUtil.getStageMap(stages);

        if (!currentStage.isPresent()) {
            return;
        }
        Stage stage = currentStage.get();
        String stageDesc = stage.getDesc();
        System.out.println("当前赛段是:" + stage.getDesc() + "   赛段编号:" + stage.getStageNo() + " 开始时间:" + stage.getStageStart() + " 结束时间:" + stage.getStageEnd());
        Optional<Stage> opPreStage = DanceKingRankUtil.getPreStage(currentStage, stageMap);
        if (!opPreStage.isPresent()) {
            System.out.println("今天不存在晋级逻辑,return");
            return;
        }
        Stage preStage = opPreStage.get();
        String preStageDesc = preStage.getDesc();
        Map<Integer, Integer> trackMap = preStage.getTrack();
        trackMap.forEach((trackId, promotionCount) -> {
            System.out.println("trackId:" + trackId + " promotionCount:" + promotionCount);
            String text = String.format("当前赛段是: %s; 上一个赛段是:%s;赛道ID:%s,需要晋级%d 人,触发晋级开始", stageDesc, preStageDesc, promotionCount);
            System.out.println(text);

        });
//        System.out.println("上一个赛段是:" + preStage.getDesc() + "   赛段编号:" + preStage.getStageNo() + " 开始时间:" + preStage.getStageStart() + " 结束时间:" + preStage.getStageEnd() + "" +
//                "大赛段晋级人数:" + preStage.getBigProCount() + " 小赛段晋级人数:" + preStage.getLittlePromCount() + " 本赛段总人数:" + preStage.getBaseCount());
//        Integer littlePromCount = preStage.getLittlePromCount();
//        Integer bigProCount = preStage.getBigProCount();

        if (!stage.isIfFinal()) {//非决赛
            //

        }
    }
}
